package com.zwan.oam_rtc.utils.wintcp;

import java.io.UnsupportedEncodingException;
import java.nio.ByteOrder;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;

public class ByteTransfer {
	public static String CHARSET = "UTF-8";
	public static ByteOrder BYTEORDER = ByteOrder.LITTLE_ENDIAN;//字符顺序从低到高

	// 用于设置于SLP通信的结构 对于为null的字符串将设置为大小为0的字节数组
	public static byte[] trans(String s, int len) {
		if (len <= 0) {
			return new byte[0];
		}
		byte[] b = new byte[len];

		if (s == null) {
			return b;
		} else {
			byte[] src;

			try {
				src = s.getBytes(CHARSET);
				System.arraycopy(src, 0, b, 0, Math.min(src.length, len - 1));
			} catch (UnsupportedEncodingException e1) {
				e1.printStackTrace();
			}
		}

		return b;
	}

	// 字符串转换为字节数组，如果字符串太长将被截断
	public static int str2Byte(String s, byte[] b) {
		Arrays.fill(b, (byte) 0);
		if (s == null)
			return 0;
		int i = 0, j = 0;
		byte[] src;
		try {
			src = s.getBytes(CHARSET);
			if (src.length <= b.length) {
				System.arraycopy(src, 0, b, 0, Math.min(src.length, b.length));
				i = src.length;
			} else {
				char c = s.charAt(i);
				while (i < s.length()) {
					c = s.charAt(i++);
					int charLen = Character.toString(c).getBytes(CHARSET).length;
					// j += charLen;
					if (j + charLen <= b.length) {
						// j -= Character.toString(c).getBytes(CHARSET).length;
						j += charLen;
					} else
						break;
				}
				System.arraycopy(src, 0, b, 0, j);
			}
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return i;
	}

	public static String byte2Str(byte[] bytes) {
		return getString(bytes);
	}

	 /**
	  * 用于获取于SLP通信的字符串的结构 获取的字符串只能是String或""
	  * @param bytes
	  * @return
	  */
	public static String getString(byte[] bytes) {
		if (bytes == null) {
			return "";
		}

		StringBuffer sb = null;

		try {
			sb = new StringBuffer(new String(bytes, CHARSET));
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}

		int i = sb.indexOf("\0");

		if (i == 0) {
			return "";
		}

		String temp;

		if (i > 0) {
			temp = sb.substring(0, i);
		} else {
			temp = sb.substring(0, Math.min(bytes.length, sb.length()));
		}
		return temp;
	}

	public static String getUTF8String(byte[] bytes) {
		if (bytes == null) {
			return "";
		}

		StringBuffer sb = null;

		sb = new StringBuffer(new String(bytes, StandardCharsets.UTF_8));

		int i = sb.indexOf("\0");

		if (i == 0) {
			return "";
		}

		String temp;

		if (i > 0) {
			temp = sb.substring(0, i);
		} else {
			temp = sb.substring(0, Math.min(bytes.length, sb.length()));
		}
		return temp;
	}

	public static String getString(byte[] bytes, String charset) {
		if (bytes == null) {
			return "";
		}

		StringBuffer sb = null;

		try {
			sb = new StringBuffer(new String(bytes, charset));
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}

		int i = sb.indexOf("\0");

		if (i == 0) {
			return "";
		}

		String temp;

		if (i > 0) {
			temp = sb.substring(0, i);
		} else {
			temp = sb.substring(0, Math.min(bytes.length, sb.length()));
		}
		return temp;
	}

	public static String byte2Str(byte[] bytes, String charset) {
		return getString(bytes, charset);
	}
	
	// 字符串转换为字节数组，如果字符串太长将被截断
	public static int str2Byte(String s, byte[] b, String charset) {
		Arrays.fill(b, (byte) 0);
		if (s == null)
			return 0;
		int i = 0, j = 0;
		byte[] src;
		try {
			src = s.getBytes(charset);
			if (src.length <= b.length) {
				System.arraycopy(src, 0, b, 0, Math.min(src.length, b.length));
				i = src.length;
			} else {
				char c = s.charAt(i);
				while (i < s.length()) {
					c = s.charAt(i++);
					int charLen = Character.toString(c).getBytes(charset).length;
					if (j + charLen <= b.length) {
						j += charLen;
					} else
						break;
				}
				System.arraycopy(src, 0, b, 0, j);
			}
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return i;
	}

//	public static void main(String[] args) {
//		String s = "000000000000009121411353400002";
//		//s = "12";
//		byte b[] = new byte[30];
//		str2Byte(s, b);
//		System.out.println(byte2Str(b));
//	}
}
